#include "employeedialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QDate>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>

EmployeeDialog::EmployeeDialog(QWidget *parent)
    : QDialog(parent), m_isEditMode(false)
{
    setWindowTitle("员工信息");
    setFixedSize(400, 300);
    setupUI();
    
    // 连接验证信号
    connect(m_nameEdit, &QLineEdit::textChanged, this, &EmployeeDialog::validateInput);
    connect(m_employeeIdEdit, &QLineEdit::textChanged, this, &EmployeeDialog::validateInput);
}

void EmployeeDialog::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    // 创建表单布局
    QFormLayout *formLayout = new QFormLayout();
    
    // 员工编号
    m_employeeIdEdit = new QLineEdit();
    formLayout->addRow("员工编号:", m_employeeIdEdit);
    
    // 姓名
    m_nameEdit = new QLineEdit();
    formLayout->addRow("姓名:", m_nameEdit);
    
    // 班组
    m_teamCombo = new QComboBox();
    m_teamCombo->setEditable(true);
    loadDepartmentData(); // 动态加载班组数据
    formLayout->addRow("所属班组:", m_teamCombo);
    
    // 入职日期
    m_hireDateEdit = new QDateEdit();
    m_hireDateEdit->setDate(QDate::currentDate());
    m_hireDateEdit->setCalendarPopup(true);
    formLayout->addRow("入职日期:", m_hireDateEdit);
    
    // 联系方式
    m_contactEdit = new QLineEdit();
    formLayout->addRow("联系方式:", m_contactEdit);
    
    // 状态
    m_statusCombo = new QComboBox();
    m_statusCombo->addItems({"在职", "离职"});
    formLayout->addRow("状态:", m_statusCombo);
    
    mainLayout->addLayout(formLayout);
    
    // 按钮
    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    mainLayout->addWidget(m_buttonBox);
    
    connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
    
    // 初始验证
    validateInput();
}

void EmployeeDialog::setEmployeeData(const QString &employeeId, const QString &name, 
                                   const QString &team, const QString &hireDate, 
                                   const QString &contact, const QString &status)
{
    m_employeeIdEdit->setText(employeeId);
    m_nameEdit->setText(name);
    
    // 设置班组
    int teamIndex = m_teamCombo->findText(team);
    if (teamIndex >= 0) {
        m_teamCombo->setCurrentIndex(teamIndex);
    } else {
        m_teamCombo->setCurrentText(team);
    }
    
    // 设置日期
    QDate date = QDate::fromString(hireDate, "yyyy-MM-dd");
    if (date.isValid()) {
        m_hireDateEdit->setDate(date);
    }
    
    m_contactEdit->setText(contact);
    
    // 设置状态
    int statusIndex = m_statusCombo->findText(status);
    if (statusIndex >= 0) {
        m_statusCombo->setCurrentIndex(statusIndex);
    }
}

QString EmployeeDialog::getEmployeeId() const
{
    return m_employeeIdEdit->text().trimmed();
}

QString EmployeeDialog::getName() const
{
    return m_nameEdit->text().trimmed();
}

QString EmployeeDialog::getTeam() const
{
    return m_teamCombo->currentText();
}

QString EmployeeDialog::getHireDate() const
{
    return m_hireDateEdit->date().toString("yyyy-MM-dd");
}

QString EmployeeDialog::getContact() const
{
    return m_contactEdit->text().trimmed();
}

QString EmployeeDialog::getStatus() const
{
    return m_statusCombo->currentText();
}

void EmployeeDialog::setEditMode(bool editMode)
{
    m_isEditMode = editMode;
    m_employeeIdEdit->setReadOnly(editMode);
    
    if (editMode) {
        setWindowTitle("编辑员工信息");
    } else {
        setWindowTitle("添加员工");
    }
}

void EmployeeDialog::validateInput()
{
    bool valid = !m_employeeIdEdit->text().trimmed().isEmpty() && 
                 !m_nameEdit->text().trimmed().isEmpty();
    
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
}

// 加载班组数据 - 新增
void EmployeeDialog::loadDepartmentData()
{
    m_teamCombo->clear();
    
    // 查询数据库中的班组数据
    QSqlQuery query("SELECT name FROM departments ORDER BY name");
    
    while (query.next()) {
        QString departmentName = query.value(0).toString();
        m_teamCombo->addItem(departmentName);
    }
    
    // 如果没有班组数据，添加默认选项
    if (m_teamCombo->count() == 0) {
        QSqlQuery insertQuery;
        // 添加默认班组
        QStringList defaultDepts = {"毛绒组", "塑料组", "包装组", "质检组"};
        
        for (const QString &dept : defaultDepts) {
            insertQuery.prepare("INSERT OR IGNORE INTO departments (name) VALUES (?)");
            insertQuery.addBindValue(dept);
            if (insertQuery.exec()) {
                m_teamCombo->addItem(dept);
            }
        }
        
        qDebug() << "已创建默认班组数据";
    }
    
    qDebug() << QString("加载了 %1 个班组").arg(m_teamCombo->count());
}